package com.iws.wms.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;

//import org.apache.shiro.SecurityUtils;
import com.iwa.common.constants.CommonConstants;
import com.iwa.common.dto.UserToken;
import com.iwa.common.utils.JwtUtils;
//import com.iws.wms.service.UserService;
//import com.iws.wms.util.UserUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.iws.wms.entity.Log;
import com.iws.wms.repository.LogRepository;
import com.iws.wms.service.LogService;
import com.iws.wms.util.StringUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 系统日志Service实现类
 *
 * @author java1234 小锋 老师
 */
@Service("logService")
public class LogServiceImpl implements LogService {

    @Resource
    private LogRepository logRepository;


    //@Resource
    //private UserService userService;

    @Override
    public void save(Log log) {
        try {
            log.setTime(new Date()); // 设置操作日期
            HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
            //log.setUser(userService.findByUserName(UserUtils.getUserToken(request))); // 设置用户名
            logRepository.save(log);
        } catch (Exception ex) {

        }
    }

    @Override
    public List<Log> list(Log log, Integer page, Integer pageSize, Direction direction, String... properties) {
        Pageable pageable = new PageRequest(page - 1, pageSize, direction, properties);
        Page<Log> pageLog = logRepository.findAll(new Specification<Log>() {

            @Override
            public Predicate toPredicate(Root<Log> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                if (log != null) {
                    //if (log.getUser() != null && StringUtil.isNotEmpty(log.getUser().getTrueName())) {
                    //    predicate.getExpressions().add(cb.like(root.get("user").get("trueName"), "%" + log.getUser().getTrueName() + "%"));
                    //}
                    if (StringUtil.isNotEmpty(log.getType())) {
                        predicate.getExpressions().add(cb.equal(root.get("type"), log.getType()));
                    }
                    if (log.getBtime() != null) {
                        predicate.getExpressions().add(cb.greaterThanOrEqualTo(root.get("time"), log.getBtime()));
                    }
                    if (log.getEtime() != null) {
                        predicate.getExpressions().add(cb.lessThanOrEqualTo(root.get("time"), log.getEtime()));
                    }
                }
                return predicate;
            }
        }, pageable);
        return pageLog.getContent();
    }

    @Override
    public Long getCount(Log log) {
        Long count = logRepository.count(new Specification<Log>() {

            @Override
            public Predicate toPredicate(Root<Log> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                if (log != null) {
                    //if (log.getUser() != null && StringUtil.isNotEmpty(log.getUser().getTrueName())) {
                    //    predicate.getExpressions().add(cb.like(root.get("user").get("trueName"), "%" + log.getUser().getTrueName() + "%"));
                    //}
                    if (StringUtil.isNotEmpty(log.getType())) {
                        predicate.getExpressions().add(cb.equal(root.get("type"), log.getType()));
                    }
                    if (log.getBtime() != null) {
                        predicate.getExpressions().add(cb.greaterThanOrEqualTo(root.get("time"), log.getBtime()));
                    }
                    if (log.getEtime() != null) {
                        predicate.getExpressions().add(cb.lessThanOrEqualTo(root.get("time"), log.getEtime()));
                    }
                }
                return predicate;
            }
        });
        return count;
    }

}
